package org.example;

import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.AspectJMethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.cglib.core.DebuggingClassWriter;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/fengliulin/IdeaProjects/redis-learn-rabbit");
        AspectJExpressionPointcut a = new AspectJExpressionPointcut();
        a.setExpression("execution(* method1())");

        MethodInterceptor methodInterceptor = invocation -> {
            System.out.println("before");
            Object proceed = invocation.proceed();
            System.out.println("after");
            return proceed;
        };

        DefaultPointcutAdvisor d = new DefaultPointcutAdvisor(a, methodInterceptor);



        T1 t1 = new T1();
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(t1);
        proxyFactory.addAdvisor(d);
        T1 proxy = (T1) proxyFactory.getProxy();

        proxy.method1();


    }

    static class T1 {
        public void method1() {

            System.out.println("我是方法1");
        }

        public void method2() {
            System.out.println("我是方法2");
        }
    }
}
